﻿using System;
using GalaSoft.MvvmLight.Messaging;

namespace JustinAngel.Neurons.Framework
{
    public static class IoC
    {
        private static IDIContainer container = new DIContainer();

        public static void Register<TFrom, TTo>()
            where TTo : TFrom
            where TFrom : class
        {
            container.Register<TFrom, TTo>();
        }

        public static void Register(Type from, Type to)
        {
            container.Register(from, to);
        }

        public static void Register(Type from, object instance)
        {
            container.RegisterInstance(from, instance);
        }

        public static T Get<T>()
           where T : class
        {
            //return container.Resolve<T>(); 

            var result = container.Resolve<T>();

            if (result == null && typeof(T) == typeof(IMessenger))
                return (T)(object)Messenger.Default;

            return result;
        }

        public static object Get(Type type)
        {
            return container.Resolve(type);
        }

        public static void Register<T>(T instance)
        {
            container.RegisterInstance<T>(instance);
        }

        public static void BuildUp<T>(T t)
        {
            container.BuildUp(t);
        }

        public static void Clear()
        {
            container = new DIContainer();
        }

        public static bool IsRegistered(Type type)
        {
            return container.DoesRegistrationExist(type);
        }
    }

}
